package com.itheima.edu.info.subwaymanager.controller;

import com.itheima.edu.info.subwaymanager.domain.Subway;
import com.itheima.edu.info.subwaymanager.service.SubwayServiceImpl;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author 王加鑫
 * @Date 2021/5/23 19:42
 * @Version 1.0
 */
public class SubwayControllerImpl implements SubwayController {

    private SubwayServiceImpl subwayService = new SubwayServiceImpl();

    private Scanner scanner = new Scanner(System.in);


    //查询地铁站信息
    @Override
    public void selectSubway() {
        ArrayList<Subway> subways = subwayService.selectSubway();
        if(subways.size() != 0){
            System.out.println("地铁站编号 \t 地铁站名称 \t 所属路线");
            for (Subway subway : subways) {
                System.out.println(subway.getId() + "\t\t" + subway.getName() + "\t\t " + subway.getLine());
            }
        }else {
            while (true){
                System.out.println("系统信息为空!请做以下操作 1添加数据 2退出系统");
                String choice = scanner.next();
                switch (choice){
                    case "1":
                        addSubway();
                        return;
                    case "2":
                        System.out.println("退出系统");
                        System.exit(0);
                    default:
                        System.out.println("您的输入有误");
                }
            }
        }

    }

    /*
    添加地铁站信息
     */
    @Override
    public void addSubway() {
        while (true){
            System.out.println("请输入您要添加的地铁编号:");
            String id = scanner.next();
            boolean flag = subwayService.isExists(id);
            if(!flag){
                System.out.println("请输入您要添加的地铁站名称:");
                String name = scanner.next();
                System.out.println("请输入您要添加的地铁路线名称:");
                String line = scanner.next();
                subwayService.addSubway(new Subway(id,name,line));
                System.out.println("添加成功!");
                return;
            }
            System.out.println("编号已经存在,添加失败");
        }

    }

    @Override
    public void updateSubway() {

    }

    @Override
    public void deleteSubway() {

    }



}
